package com.manager.traffic.service.impl;

import com.manager.common.dao.SuperDao;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.model.Mac;
import com.manager.model.SensitiveWord;
import com.manager.traffic.service.MacService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Component
@Transactional
public class MacServiceImpl extends SuperDao<Object> implements MacService {

    @Override
    public PageVO geMacPage(Mac model) {
        String sql ="  from Mac t where 1 = 1 ";
        List<Object> params = new ArrayList<>();
        if(StringUtils.isNotEmpty(model.getMac())){
            sql += " and t.mac like # ";
            params.add("%"+model.getMac()+"%");
        }
        sql = ObjectUtils.replaceIndexByList(sql, params);
        List<Mac> list = this.getList(sql,params.toArray(),model.getStart(),model.getLimit(),"");
        int sum = this.getListCount("select count(*) " + sql, params.toArray(), "");
        int count = (int)Math.ceil(Double.valueOf(sum)/Double.valueOf(model.getLimit()));

        return PageVO.create(model.getStart(),count,list);
    }

    @Override
    public void addMac(Mac model) {
        this.addT(model);
    }

    public void updateMac(Mac model) {
        Mac mac = this.getT(model.getId(),Mac.class);
        BeanUtils.copyProperties(model,mac,new String[]{"id"});
        this.updateT(mac);
    }

    @Override
    public void deleteMac(Mac model) {
        Mac mac = this.getT(model.getId(),Mac.class);
        this.delete(mac);
    }

    @Override
    public void updateMacEnabled(Mac model) {
        Mac mac = this.getT(model.getId(),Mac.class);
        mac.setHasEnable(model.getHasEnable());
        this.updateT(mac);
    }

    @Override
    public List<Mac> getMacList() {
        String sql = " from Mac t where t.hasEnable = 1 ";
        List<Mac> list = this.getList(sql,new Object[]{},0,0,"");
        return list;
    }
}

